package com.zenghus.mybatis.demo;

import com.zenghus.mybatis.beans.Blog;
import com.zenghus.mybatis.beans.Test;
import com.zenghus.mybatis.mapper.BlogMapper;
import com.zenghus.mybatis.mapper.TestMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {
    public static void main(String[] args) throws IOException, InterruptedException {
        String resource = "E:\\workspace-idea\\mybatis\\src\\main\\resources\\mybatis-config.xml";
        InputStream inputStream = new FileInputStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlsession=sqlSessionFactory.openSession();
        TestMapper testMapper = sqlsession.getMapper(TestMapper.class);

        //验证一级缓存问题
        Test t=testMapper.selectByPrimaryKey(1);
        System.out.println(t);
        //在5秒内改变数据
        Thread.sleep(5000);
        t=testMapper.selectByPrimaryKey(1);
        System.out.println(t);


        //n+1问题
        BlogMapper blogMapper = sqlsession.getMapper(BlogMapper.class);
        Blog b=blogMapper.lazyLoading(1);
        System.out.println(b.getName());
        System.out.println(b.getAuthord().getAid());



    }
}
